﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IP.Core.IPCommon;
using AuctionUS;
using AuctionUS;
using AuctionDS;
using AuctionDS.CDBNames;

namespace AuctionForm
{
    public partial class f221_danh_sach_sv_de : Form
    {
        public f221_danh_sach_sv_de()
        {
            InitializeComponent();
            format_controls();
        }

#region Public Interface
        public void display_for_insert()
        {
            m_e_form_mode = DataEntryFormMode.InsertDataState;
            this.ShowDialog();
        }

        public void display_for_update(US_STUDENT ip_us_student)
        {
            m_us_student = ip_us_student;
            m_e_form_mode = DataEntryFormMode.UpdateDataState;
            this.ShowDialog();
        }
#endregion

#region Data Structures
#endregion

#region Members
        DataEntryFormMode m_e_form_mode;
        US_STUDENT m_us_student = new US_STUDENT();
        US_CLASS m_us_class = new US_CLASS();
#endregion

#region Private Methods
        private void format_controls()
        {
            CControlFormat.setFormStyle(this);
            set_define_events();
        }

        // phuong thuc dua du lieu len form
        private void us_object_2_form()
        {
            m_txt_name.Text = m_us_student.strNAME;
            m_txt_age.Text = CIPConvert.ToStr(m_us_student.dcAGE);
            m_txt_address.Text = m_us_student.strADDRESS;
            m_dat_birthday.Value = m_us_student.datBIRTHDAY;
            m_cbo_class.SelectedValue = m_us_student.dcCLASSID;
        }
        private void load_data_2_cbo()
        {
            US_CLASS v_us_class = new US_CLASS();
            DS_CLASS v_ds_class = new DS_CLASS();
            v_us_class.FillDataset(v_ds_class);

            m_cbo_class.DisplayMember = CLASS.CLASS_NAME;
            m_cbo_class.ValueMember = CLASS.ID;
            m_cbo_class.DataSource = v_ds_class.CLASS;
        }
        private void set_inital_form_load()
        {
            load_data_2_cbo();
            switch (m_e_form_mode)
            {
                case DataEntryFormMode.InsertDataState:
                    //load_data_2_cbo();
                    break;
                case DataEntryFormMode.UpdateDataState:
                    us_object_2_form();
                    break;
            }

        }
        private bool check_data_is_ok()
        {
            if (!CValidateTextBox.IsValid(m_txt_name, DataType.StringType, allowNull.NO,true))
            {
                return false;
            }
            if (!CValidateTextBox.IsValid(m_txt_age, DataType.NumberType, allowNull.YES, true))
            {
                return false;

            }
            if (!CValidateTextBox.IsValid(m_txt_address, DataType.StringType, allowNull.NO, true))
            {
                return false;
            }
            return true;
        }
        private void form_2_us_object()
        {
            m_us_student.strNAME = m_txt_name.Text;
            m_us_student.strADDRESS = m_txt_address.Text;
            if ( m_txt_age.Text == "")
            {
                m_us_student.SetAGENull();
            } 
            else
            {
                m_us_student.dcAGE = CIPConvert.ToDecimal(m_txt_age.Text);
            }
            m_us_student.datBIRTHDAY = m_dat_birthday.Value.Date;
            m_us_student.dcCLASSID = CIPConvert.ToDecimal(m_cbo_class.SelectedValue);
        }

        private void save_data()
        {
            if (check_data_is_ok() == false) return;
            form_2_us_object();
            switch (m_e_form_mode)
            {
                case DataEntryFormMode.InsertDataState:
                    m_us_student.Insert();
                    break;
                case DataEntryFormMode.UpdateDataState:
                    m_us_student.Update();
                    break;
            }
            BaseMessages.MsgBox_Infor("Data is updated !");
            this.Close();
        }
#endregion

        private void set_define_events()
        {
            this.m_cmd_exit.Click += new EventHandler(m_cmd_exit_Click);
            this.Load += new EventHandler(f221_danh_sach_sv_de_Load);

            this.m_cmd_save.Click += m_cmd_save_Click;
          
        }

        void m_cmd_save_Click(object sender, EventArgs e)
        {
            try
            {
                save_data();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }



        void m_cmd_exit_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }

        void f221_danh_sach_sv_de_Load(object sender, EventArgs e)
        {
            try
            {
                set_inital_form_load();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }

    }
}
